<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%
    String postId = request.getParameter("id");
    String title = "";
    String content = "";
    String category = "";
    
    if (postId != null) {
        String DB_URL = "jdbc:mysql://localhost:3306/lczmysql?useUnicode=true&characterEncoding=UTF-8";
        String DB_USER = "root";
        String DB_PASSWORD = "123456";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            String sql = "SELECT * FROM post WHERE id = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, Integer.parseInt(postId));
            ResultSet resultSet = statement.executeQuery();
            
            if (resultSet.next()) {
                title = resultSet.getString("title");
                content = resultSet.getString("content");
                category = resultSet.getString("category");
            }
            
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
%>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑帖子</title>
    <script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.all.js"></script>
    <script type="text/javascript" charset="utf-8" src="../ueditor/lang/zh-cn/zh-cn.js"></script>
</head>

<body>
    <main>
        <h1>编辑帖子</h1>
        <form action="http://localhost:8080/luntan/updatePost" method="post">
            <!-- 添加帖子ID隐藏字段 -->
            <input type="hidden" name="id" value="<%= postId %>">
            
            <!-- 帖子标题 -->
            <label for="title">帖子标题：</label>
            <input type="text" id="title" name="title" value="<%= title %>" required><br>

            <!-- 编辑器 -->
            <div>
                <script id="editor" type="text/plain" style="width: 100%; height: 600px;">
                    <%= content %>
                </script>
            </div>

            <!-- 帖子作者 -->
            <label for="post-author">作者：</label><br>
            <input type="text" id="post-author" name="author" value="${sessionScope.username}" required readonly><br><br>

            <!-- 分类 -->
            <label for="category">分类：</label><br>
            <select id="post-category" name="category">
                <option value="校园生活" <%= category.equals("校园生活") ? "selected" : "" %>>校园生活</option>
                <option value="社团活动" <%= category.equals("社团活动") ? "selected" : "" %>>社团活动</option>
            </select><br><br>

            <input type="submit" value="保存修改" />
        </form>
    </main>

    <script type="text/javascript">
        // 实例化编辑器
        var ue = UE.getEditor('editor');
    </script>
</body>

</html>
